package com.ruoyi.project.system.lotto.controller;

import cn.hutool.core.util.StrUtil;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.system.lotto.domain.*;
import com.ruoyi.project.system.lotto.service.LottoLogsService;
import com.ruoyi.project.system.lotto.service.RewardsService;
import com.ruoyi.project.system.lotto.service.UserLogsService;
import io.swagger.annotations.Api;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

/**
 * 抽奖控制器
 */
@Api("抽奖管理")
@RequestMapping("/lotto")
@CrossOrigin(allowCredentials="true",maxAge = 3600)
@RestController
public class LottoController {
    @Autowired
    private LottoLogsService lottoLogsService;
    @Autowired
    private RewardsService rewardsService;
    @Autowired
    private UserLogsService userLogsService;

    /**
     * 保存抽奖记录
     * @return
     */
    @CrossOrigin
    @PostMapping("save")
    @ResponseBody
    @Log(title = "抽奖记录", businessType = BusinessType.INSERT)
    public ResObject saveLottoLog(@RequestBody LottoLogs lotto) {
        try {
            if (lotto.getRewardId() != null && lotto.getUserId() != null) {
                UserLogs userLogs = userLogsService.selectByPrimaryKey(lotto.getUserId());
                lotto.setCreateTime(new Date());
                lotto.setUpdateTime(new Date());

                if (lotto.getRewardId() == 0) {
                    lottoLogsService.insert(lotto);
                    userLogs.setLottoCount(userLogs.getLottoCount() - 1);
                    if (userLogs.getLottoCount() < 1) {
                        userLogs.setLottoQualify(0);
                    }
                    userLogs.setLottoStatus(2);
                    userLogsService.updateByPrimaryKeySelective(userLogs);
                    return ResObject.success("谢谢参与");
                } else {
                    Rewards rewards = rewardsService.selectByPrimaryKey(lotto.getRewardId());

                    if (rewards != null && userLogs != null) {
                        lottoLogsService.insert(lotto);

                        if (lotto.getStatus() == 3 || lotto.getStatus() == 4 ) {
                            if (rewards.getStock() < 1) {
                                userLogs.setLottoCount(userLogs.getLottoCount() - 1);
                                if (userLogs.getLottoCount() < 1) {
                                    userLogs.setLottoQualify(0);
                                }

                                userLogs.setLottoStatus(lotto.getStatus());
                                userLogsService.updateByPrimaryKeySelective(userLogs);
                                return ResObject.error("奖品已被抽完");
                            } else {
                                userLogs.setLottoStatus(lotto.getStatus());
                                userLogs.setLottoQualify(0);
                                userLogs.setLottoCount(userLogs.getLottoCount() - 1);
                                if (userLogs.getLottoCount() < 1) {
                                    userLogs.setLottoQualify(0);
                                }
                                userLogsService.updateByPrimaryKeySelective(userLogs);
                                rewards.setStock(rewards.getStock() - 1);
                                rewardsService.updateByPrimaryKey(rewards);
                            }
                        }
                    } else {
                        return ResObject.error("无此奖品或用户");
                    }

                    return ResObject.success("抽奖成功");
                }
            } else {
                return ResObject.error("奖品和用户不可为空");
            }
        } catch (Exception e) {
            return ResObject.error("请重新抽奖");
        }
    }

    /**
     * 获取抽奖记录
     * @return
     */
    @CrossOrigin
    @PostMapping("getRecords")
    @ResponseBody
    public ResObject getRecords(@RequestBody LottoLogs lottoLogs) {
        if (lottoLogs.getUserId() != null) {
            List<RecordVo> records = lottoLogsService.getRecords(lottoLogs.getUserId());
            if (!records.isEmpty()) {
                records.forEach((record) -> {
                    if (StrUtil.isBlank(record.getRewardName())) {
                        record.setRewardName("未中奖");
                    }
                });
            }
            return ResObject.success("查询成功", records);
        } else {
            return ResObject.error("用户id不可为空");
        }
    }
}
